package com.lijian.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijian.common.easyui.datagrid.EasyuiDataGridResult;
import com.lijian.common.utils.IDUtils;
import com.lijian.common.utils.JsonUtils;
import com.lijian.common.utils.TaotaoResult;
import com.lijian.mapper.TbItemDescMapper;
import com.lijian.mapper.TbItemMapper;
import com.lijian.mapper.TbItemParamItemMapper;
import com.lijian.pojo.TbItem;
import com.lijian.pojo.TbItemDesc;
import com.lijian.pojo.TbItemExample;
import com.lijian.pojo.TbItemParamItem;
import com.lijian.pojo.TbItemParamItemExample;
import com.lijian.pojo.TbItemParamItemExample.Criteria;
import com.lijian.service.ItemService;
import com.sun.xml.internal.xsom.impl.scd.Iterators.Map;

@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;
	@Override
	public TbItem getItemById(Long itemId) {
		return itemMapper.selectByPrimaryKey(itemId);
	}
	@Override
	public EasyuiDataGridResult getItemList(int page, int rows) {
		TbItemExample example=new TbItemExample();
		PageHelper.startPage(page, rows);
		List<TbItem> list=itemMapper.selectByExample(example);
		PageInfo<TbItem> pageInfo=new PageInfo<TbItem>(list);
		return new EasyuiDataGridResult(pageInfo.getTotal(), list);
	}
	@Override
	public TaotaoResult createItem(TbItem item, String desc,String itemParamData) {
		Long itemId=IDUtils.genItemId();
		item.setId(itemId);
		item.setStatus((byte)1);//商品状态，1-正常，2-下架，3-删除
		item.setCreated(new Date());
		item.setUpdated(new Date());
		itemMapper.insert(item);
		TbItemDesc itemDesc=new TbItemDesc();
		itemDesc.setItemId(itemId);
		itemDesc.setItemDesc(desc);
		item.setCreated(new Date());
		item.setUpdated(new Date());
		
		saveItemParamData(itemId,itemParamData);
		
		itemDescMapper.insert(itemDesc);
		return TaotaoResult.ok();
	}
	public void saveItemParamData(Long itemId, String itemParamData){
		TbItemParamItem item=new TbItemParamItem();
		item.setItemId(itemId);
		item.setParamData(itemParamData);
		item.setCreated(new Date());
		item.setUpdated(new Date());
		
		itemParamItemMapper.insert(item);
	}
	@Override
	public String getItemParamHtml(Long itemId) {
		TbItemParamItemExample example=new TbItemParamItemExample();
		Criteria criteria= example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemParamItem> list=itemParamItemMapper.selectByExampleWithBLOBs(example);
		if(list==null || list.isEmpty()){
			return "";
		}
		TbItemParamItem tbItemParamItem = list.get(0);
		String paramData=tbItemParamItem.getParamData();
		List<Map> jsonToList = JsonUtils.jsonToList(paramData, Map.class);
		for (int i = 0; i < jsonToList.size(); i++) {
			
		}
		return null;
	}

}
